package com.ourteam.system.dao.impl;

import com.ourteam.system.dao.ISysRoleGroupDAO;
import com.ourteam.system.dao.SysRoleGroup;
import com.ourteam.system.dao.SysRoleGroupDAOQueryBean;

import net.dao.AbstractSpringDAO;
import net.dao.DataAccessObjectHelper;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;

import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * Template Name : DAO服务对象
 * Template Version : 1
 * Template Last Modfiy Date : Fri Nov 17 11:00:11 CST 2017
 * Auto Generate Date : 2018-07-31 15:13:36
  * Warning ! This file will be overwrite when regenerate !
  *
 */
public class AbstractSysRoleGroupDAOJdbcImpl extends AbstractSpringDAO
    implements ISysRoleGroupDAO {
    public static final String[] ENTITY_FIELDS = new String[] {
            Id, Remarks, RoleCode, RoleName, RoleType, Status, DefaultFlag,
            ModifyTime, OperatorId, OperatorName
        };
    public static final String[] ENTITY_PROPERTIES = new String[] {
            _DataObjectProperty.Id, _DataObjectProperty.Remarks,
            _DataObjectProperty.RoleCode, _DataObjectProperty.RoleName,
            _DataObjectProperty.RoleType, _DataObjectProperty.Status,
            _DataObjectProperty.DefaultFlag, _DataObjectProperty.ModifyTime,
            _DataObjectProperty.OperatorId, _DataObjectProperty.OperatorName
        };
    public static final String[] ENTITY_PK_FIELDS = new String[] { Id };
    public static final String[] ENTITY_AUTO_GEN_PK_FIELDS = new String[] { Id };
    public static final String[] ENTITY_UNIQUE_FIELDS = new String[] {  };
    private static String relatedJoinSQL = null;
    private static String basicFilterSQL = null;
    private DataAccessObjectHelper<SysRoleGroup, SysRoleGroupDAOQueryBean> daoHelper;
    private com.ourteam.system.dao.impl.AbstractSysRoleRelateDAOJdbcImpl SysRoleRelateDao;

    public AbstractSysRoleGroupDAOJdbcImpl() {
        super();
        daoHelper = new DataAccessObjectHelper<SysRoleGroup, SysRoleGroupDAOQueryBean>(this);
    }

    protected String getEntityName() {
        return EntityName;
    }

    protected String[] getEntityFields() {
        return ENTITY_FIELDS;
    }

    protected String[] getEntityProperties() {
        return ENTITY_PROPERTIES;
    }

    protected String[] getEntityPKFields() {
        return ENTITY_PK_FIELDS;
    }

    protected String[] getEntityAutoGenPKFields() {
        return ENTITY_AUTO_GEN_PK_FIELDS;
    }

    protected String[] getEntityUniqueFields() {
        return ENTITY_UNIQUE_FIELDS;
    }

    protected String getBaiscQueryFilter() {
        if (basicFilterSQL == null) {
            StringBuffer basicFilterSQLbuffer = new StringBuffer();

            basicFilterSQLbuffer.append(RoleType).append("= 'ROLE_GROUP' ");

            basicFilterSQLbuffer.append(" AND ");

            basicFilterSQLbuffer.append(Status).append("= 'active' ");

            basicFilterSQL = basicFilterSQLbuffer.toString();
        }

        return basicFilterSQL;
    }

    protected String getRelatedJoinSQL() {
        if (relatedJoinSQL == null) {
            StringBuffer relatedJoinSQLBuffer = new StringBuffer();

            relatedJoinSQLBuffer.append(" LEFT JOIN ")
                                .append(com.ourteam.system.dao.ISysUserDAO.EntityName)
                                .append(" operator_user ");

            relatedJoinSQLBuffer.append(" ON ( ");

            relatedJoinSQLBuffer.append(StringUtils.replace(
                    com.ourteam.system.dao.ISysUserDAO.Id,
                    com.ourteam.system.dao.ISysUserDAO.EntityName + ".",
                    "operator_user."));
            relatedJoinSQLBuffer.append(" = ").append(OperatorId);

            relatedJoinSQLBuffer.append(" ) ");

            relatedJoinSQL = relatedJoinSQLBuffer.toString();
        }

        return relatedJoinSQL;
    }

    /**
         * Check Data Unique
         * @param aSysRoleGroup
         * @throws Exception
         */
    public boolean checkIsUnique(SysRoleGroup aSysRoleGroup)
        throws Exception {
        if (aSysRoleGroup == null) {
            return true;
        }

        List valueList = new ArrayList();

        List pkValueList = new ArrayList();

        pkValueList.add(aSysRoleGroup.getId());

        return isUnique(valueList.toArray(new Object[valueList.size()]),
            pkValueList.toArray(new Object[pkValueList.size()]));
    }

    /**
     * Insert Data
     * @param aSysRoleGroup
     * @throws Exception
     */
    public void insert(SysRoleGroup aSysRoleGroup) throws Exception {
        PropertyUtils.setProperty(aSysRoleGroup, _DataObjectProperty.RoleType,
            org.apache.commons.beanutils.ConvertUtils.convert("ROLE_GROUP",
                String.class));

        PropertyUtils.setProperty(aSysRoleGroup, _DataObjectProperty.Status,
            org.apache.commons.beanutils.ConvertUtils.convert("active",
                String.class));

        super.doInsert(aSysRoleGroup);
    }

    /**
         * Batch Insert Datas
         * @param aSysRoleGroup
         * @throws Exception
         */
    public void batchInsert(SysRoleGroup[] aSysRoleGroups)
        throws Exception {
        super.doBatchInsert(aSysRoleGroups);
    }

    /**
         * Delete Data
         * @param  final long id         *
         * @throws Exception
         */
    public void delete(final long id) throws Exception {
        SysRoleGroup tempSysRoleGroup = findById(id);

        PropertyUtils.setProperty(tempSysRoleGroup, _DataObjectProperty.Status,
            org.apache.commons.beanutils.ConvertUtils.convert("deactive",
                String.class));

        update(tempSysRoleGroup);
    }

    /**
     * Delete Datas
     * @param queryBean
     * @throws Exception
     */
    public void delete(SysRoleGroupDAOQueryBean queryBean)
        throws Exception {
        SysRoleGroup tempSysRoleGroup = new SysRoleGroup();

        PropertyUtils.setProperty(tempSysRoleGroup, _DataObjectProperty.Status,
            org.apache.commons.beanutils.ConvertUtils.convert("deactive",
                String.class));

        update(tempSysRoleGroup, queryBean);
    }

    public void delete(SysRoleGroup aSysRoleGroup) throws Exception {
        delete(aSysRoleGroup.getId());
    }

    /**
     * Update Data Selective
     * @param aSysRoleGroup
     * @throws Exception
     */
    public void update(SysRoleGroup aSysRoleGroup) throws Exception {
        if (aSysRoleGroup.getId() == 0) {
            throw new Exception(
                "Can not update object data whith out primary key value");
        }

        super.doUpdate(aSysRoleGroup);

        SysRoleGroup tempObj = findById(aSysRoleGroup.getId());

        if (tempObj != null) {
            PropertyUtils.copyProperties(aSysRoleGroup, tempObj);
        }
    }

    /**
     * Batch Update Datas
     * @param aSysRoleGroups[]
     * @throws Exception
     */
    public void batchUpdate(SysRoleGroup[] aSysRoleGroups)
        throws Exception {
        super.doBatchUpdate(aSysRoleGroups);
    }

    /**
         * Update Data By Condictions
         * @param aSysRoleGroup
         * @param aValueCriteria
         * @param aTransaction
         * @throws Exception
         */
    public void update(SysRoleGroup aSysRoleGroup,
        SysRoleGroupDAOQueryBean aQueryBean) throws Exception {
        super.doUpdateByQuery(aSysRoleGroup, aQueryBean);
    }

    /**
     * Update Data By Condictions
     * @param aQueryBean
     * @throws Exception
     */
    public void update(SysRoleGroupDAOQueryBean aQueryBean)
        throws Exception {
        super.doUpdateByQuery(aQueryBean);
    }

    protected void checkNullValue(SysRoleGroup aSysRoleGroup)
        throws Exception {
        if (aSysRoleGroup.getDefaultFlag() == null) {
            throw new Exception(
                "The SysRoleGroup.defaultFlag value is not allow NULL");
        }

        if (aSysRoleGroup.getModifyTime() == null) {
            throw new Exception(
                "The SysRoleGroup.modifyTime value is not allow NULL");
        }

        if (aSysRoleGroup.getOperatorName() == null) {
            throw new Exception(
                "The SysRoleGroup.operatorName value is not allow NULL");
        }

        if (aSysRoleGroup.getRemarks() == null) {
            throw new Exception(
                "The SysRoleGroup.remarks value is not allow NULL");
        }

        if (aSysRoleGroup.getRoleName() == null) {
            throw new Exception(
                "The SysRoleGroup.roleName value is not allow NULL");
        }

        if (aSysRoleGroup.getStatus() == null) {
            throw new Exception(
                "The SysRoleGroup.status value is not allow NULL");
        }
    }

    /**
     * Get Data By ID
     * @param  final long id         * @return  ${businessObject.name}
     * @throws Exception
     */
    public SysRoleGroup findById(final long id) throws Exception {
        SysRoleGroupDAOQueryBean queryBean = new SysRoleGroupDAOQueryBean();

        SysRoleGroupDAOQueryBean.Criteria criteria = queryBean.createCriteria();

        criteria.andIdEqualTo(id);

        //queryBean.setDistinct(true);
        SysRoleGroup[] SysRoleGroups = query(queryBean);

        if (SysRoleGroups.length == 0) {
            return null;
        } else {
            return SysRoleGroups[0];
        }
    }

    /**
         * Query Data
         * @param queryBean
         * @return SysRoleGroup[]
         * @throws Exception
         */
    public SysRoleGroup[] query(SysRoleGroupDAOQueryBean queryBean)
        throws Exception {
        List result = super.doQuery(queryBean, new JdbcSysRoleGroupRowMapper());

        return (SysRoleGroup[]) result.toArray(new SysRoleGroup[result.size()]);
    }

    /**
     * Query ${businessObject.name} Selective
     * @param queryBean
     * @return Map[]
     * @model
     * @throws Exception
     */
    public Map<String, Object>[] querySelective(
        SysRoleGroupDAOQueryBean queryBean) throws Exception {
        if (queryBean.getSelectProperties().isEmpty()) {
            queryBean.addAllSelectProperties();
        }

        return super.doQuerySelective(queryBean);
    }

    /**
     * Query Data Count
     * @param queryBean
     * @return int
     * @throws Exception
     */
    public int queryCount(SysRoleGroupDAOQueryBean queryBean)
        throws Exception {
        return super.doQueryCount(queryBean);
    }

    public void synchronize(SysRoleGroup[] aSysRoleGroups,
        SysRoleGroupDAOQueryBean queryBean) throws Exception {
        daoHelper.synchronize(aSysRoleGroups, queryBean);
    }

    public void synchronize(SysRoleGroup[] aSysRoleGroups,
        SysRoleGroupDAOQueryBean queryBean,
        net.dao.IDataAccessInterface.ISynchronizeCallback<SysRoleGroup> callBack)
        throws Exception {
        daoHelper.synchronize(aSysRoleGroups, queryBean, callBack);
    }

    public void setSysRoleRelateDao(
        com.ourteam.system.dao.ISysRoleRelateDAO SysRoleRelateDao) {
        this.SysRoleRelateDao = (com.ourteam.system.dao.impl.AbstractSysRoleRelateDAOJdbcImpl) SysRoleRelateDao;
    }

    /**
     * Query Related SysRoleRelate
    * @param queryBean
     * @throws Exception
     */
    public com.ourteam.system.dao.SysRoleRelate[] querySysRoleRelate(
        SysRoleGroupDAOQueryBean queryBean) throws Exception {
        StringBuffer joinSQL = new StringBuffer();

        joinSQL.append(" LEFT JOIN ").append(EntityName).append(" ON ( ");

        joinSQL.append(com.ourteam.system.dao.ISysRoleRelateDAO.SysRoleGroupId)
               .append(" = ").append(Id);

        joinSQL.append(" ) ");

        String entityName = com.ourteam.system.dao.impl.SysRoleRelateDAO.EntityName +
            " ref_SysRoleRelate";

        String[] entityFields = com.ourteam.system.dao.impl.SysRoleRelateDAO.ENTITY_FIELDS;

        String[] entityProperties = com.ourteam.system.dao.impl.SysRoleRelateDAO.ENTITY_PROPERTIES;

        List list = SysRoleRelateDao.doQuery(entityName, entityFields,
                entityProperties, queryBean, joinSQL.toString(),
                new com.ourteam.system.dao.impl.SysRoleRelateDAO.JdbcSysRoleRelateRowMapper());

        return (com.ourteam.system.dao.SysRoleRelate[]) list.toArray(new com.ourteam.system.dao.SysRoleRelate[list.size()]);
    }

    /**
     * Query Related SysRoleRelate Selective
     * @param queryBean
     * @return Map[]
     * @model
     * @throws Exception
     */
    public Map<String, Object>[] querySysRoleRelateSelective(
        SysRoleGroupDAOQueryBean queryBean) throws Exception {
        if (queryBean.getSelectProperties().isEmpty()) {
            queryBean.addSelectProperty(com.ourteam.system.dao.ISysRoleRelateDAO.Id,
                "id");

            queryBean.addSelectProperty(com.ourteam.system.dao.ISysRoleRelateDAO.RelatedSysRoleId,
                "relatedSysRoleId");

            queryBean.addSelectProperty(com.ourteam.system.dao.ISysRoleRelateDAO.Remarks,
                "remarks");

            queryBean.addSelectProperty(com.ourteam.system.dao.ISysRoleRelateDAO.Status,
                "status");

            queryBean.addSelectProperty(com.ourteam.system.dao.ISysRoleRelateDAO.SysRoleGroupId,
                "sysRoleGroupId");

            queryBean.addSelectProperty(com.ourteam.system.dao.ISysRoleRelateDAO.RelatedSysRoleCode,
                "relatedSysRoleCode");
        }

        StringBuffer joinSQL = new StringBuffer();

        joinSQL.append(" LEFT JOIN ").append(EntityName).append(" ON ( ");

        joinSQL.append(com.ourteam.system.dao.ISysRoleRelateDAO.SysRoleGroupId)
               .append(" = ").append(Id);

        joinSQL.append(" ) ");

        String entityName = com.ourteam.system.dao.impl.SysRoleRelateDAO.EntityName +
            " ref_SysRoleRelate";

        return SysRoleRelateDao.doQuerySelective(entityName, queryBean,
            joinSQL.toString());
    }

    /**
     * Update Related SysRoleRelate
     * @param aSysRoleRelate
     * @param aQueryBean
     * @model
     * @throws Exception
     */
    public void updateSysRoleRelate(
        com.ourteam.system.dao.SysRoleRelate aSysRoleRelate,
        SysRoleGroupDAOQueryBean aQueryBean) throws Exception {
        StringBuffer joinSQL = new StringBuffer();

        joinSQL.append(" LEFT JOIN ").append(EntityName).append(" ON ( ");

        joinSQL.append(com.ourteam.system.dao.ISysRoleRelateDAO.SysRoleGroupId)
               .append(" = ").append(Id);

        joinSQL.append(" ) ");

        String entityName = com.ourteam.system.dao.impl.SysRoleRelateDAO.EntityName +
            " ref_SysRoleRelate";

        String[] entityFields = com.ourteam.system.dao.impl.SysRoleRelateDAO.ENTITY_FIELDS;

        String[] entityProperties = com.ourteam.system.dao.impl.SysRoleRelateDAO.ENTITY_PROPERTIES;

        SysRoleRelateDao.doUpdateByQuery(entityName, entityFields,
            entityProperties, joinSQL.toString(), aSysRoleRelate, aQueryBean);
    }

    /**
     * Delete Related SysRoleRelate
     * @param aQueryBean
     * @model
     * @throws Exception
     */
    public void deleteSysRoleRelate(SysRoleGroupDAOQueryBean aQueryBean)
        throws Exception {
        StringBuffer joinSQL = new StringBuffer();

        joinSQL.append(" LEFT JOIN ").append(EntityName).append(" ON ( ");

        joinSQL.append(com.ourteam.system.dao.ISysRoleRelateDAO.SysRoleGroupId)
               .append(" = ").append(Id);

        joinSQL.append(" ) ");

        String entityName = com.ourteam.system.dao.impl.SysRoleRelateDAO.EntityName; // + " ref_SysRoleRelate";

        SysRoleRelateDao.doDeleteByQuery(entityName, joinSQL.toString(),
            aQueryBean);
    }

    public static class JdbcSysRoleGroupRowMapper implements RowMapper<SysRoleGroup> {
        public SysRoleGroup mapRow(ResultSet resultSet, int arg1)
            throws SQLException {
            SysRoleGroup object = new SysRoleGroup();

            int columnIndex = 1;

            int __dataType;

            try {
                Object id = null;

                id = resultSet.getObject(columnIndex++);

                object.setIdNull(resultSet.getBoolean(columnIndex++));

                if (id != null) {
                    setBeanValue(object, _DataObjectProperty.Id, id);
                }

                Object remarks = null;

                remarks = resultSet.getObject(columnIndex++);

                object.setRemarksNull(resultSet.getBoolean(columnIndex++));

                if (remarks != null) {
                    setBeanValue(object, _DataObjectProperty.Remarks, remarks);
                }

                Object roleCode = null;

                roleCode = resultSet.getObject(columnIndex++);

                object.setRoleCodeNull(resultSet.getBoolean(columnIndex++));

                if (roleCode != null) {
                    setBeanValue(object, _DataObjectProperty.RoleCode, roleCode);
                }

                Object roleName = null;

                roleName = resultSet.getObject(columnIndex++);

                object.setRoleNameNull(resultSet.getBoolean(columnIndex++));

                if (roleName != null) {
                    setBeanValue(object, _DataObjectProperty.RoleName, roleName);
                }

                Object roleType = null;

                roleType = resultSet.getObject(columnIndex++);

                object.setRoleTypeNull(resultSet.getBoolean(columnIndex++));

                if (roleType != null) {
                    setBeanValue(object, _DataObjectProperty.RoleType, roleType);
                }

                Object status = null;

                status = resultSet.getObject(columnIndex++);

                object.setStatusNull(resultSet.getBoolean(columnIndex++));

                if (status != null) {
                    setBeanValue(object, _DataObjectProperty.Status, status);
                }

                Object defaultFlag = null;

                defaultFlag = resultSet.getObject(columnIndex++);

                object.setDefaultFlagNull(resultSet.getBoolean(columnIndex++));

                if (defaultFlag != null) {
                    setBeanValue(object, _DataObjectProperty.DefaultFlag,
                        defaultFlag);
                }

                Object modifyTime = null;

                __dataType = resultSet.getMetaData().getColumnType(columnIndex);

                if ((__dataType == java.sql.Types.DATE) ||
                        (__dataType == java.sql.Types.TIME) ||
                        (__dataType == java.sql.Types.TIMESTAMP)) {
                    modifyTime = resultSet.getTimestamp(columnIndex++);
                } else {
                    modifyTime = resultSet.getObject(columnIndex++);
                }

                object.setModifyTimeNull(resultSet.getBoolean(columnIndex++));

                if (modifyTime != null) {
                    setBeanValue(object, _DataObjectProperty.ModifyTime,
                        modifyTime);
                }

                Object operatorId = null;

                operatorId = resultSet.getObject(columnIndex++);

                object.setOperatorIdNull(resultSet.getBoolean(columnIndex++));

                if (operatorId != null) {
                    setBeanValue(object, _DataObjectProperty.OperatorId,
                        operatorId);
                }

                Object operatorName = null;

                operatorName = resultSet.getObject(columnIndex++);

                object.setOperatorNameNull(resultSet.getBoolean(columnIndex++));

                if (operatorName != null) {
                    setBeanValue(object, _DataObjectProperty.OperatorName,
                        operatorName);
                }

                return object;
            } catch (Exception e) {
                throw new SQLException(e.getMessage());
            }
        }
    }
}
